// 我的频道
import { getAllChannels } from '@/api/channel'
// 所有频道
import { getAllChannel } from '@/api/channel'
import { updateMyChannels } from '@/api/channel'
import {
  getMyChannels as getMyChannelsWithLocal,
  setMyChannels as setMyChannelsWithLocal
}
  from '@/utils/storage.js'
import { delMyChannel } from '@/api/channel'
export default {
  namespaced: true,
  state: {
    isShowPopup: false,
    activeChannelId: 0, // 默认为推荐频道，id为0
    myChannels: [],
    allChannels: []
  },
  mutations: {
    setActiveChannelId (state, newActiveChannelId) {
      state.activeChannelId = newActiveChannelId
    },
    setMyChannels (state, newMyChannels) {
      state.myChannels = newMyChannels
    },
    showPopup (state) {
      state.isShowPopup = true
    },
    hidePopup (state) {
      state.isShowPopup = false
    },
    setAllChannels (state, newAllChannels) {
      state.allChannels = newAllChannels
    }
  },
  actions: {
    async loadMyChannels (ctx) {
      if (ctx.rootState.user.token) {
        const [err, res] = await getAllChannels()
        if (!err) ctx.commit('setMyChannels', res.data.data.channels)
        return [err, res] // 返回出去供外界在调用此action时获取
      } else {
        const myChannels = await getMyChannelsWithLocal()
        if (myChannels) {
          ctx.commit('setMyChannels', myChannels)
          const res = { data: { data: { channels: myChannels } } }
          return [null, res]
        } else {
          const [err, res] = await getAllChannels()
          if (!err) {
            ctx.commit('setMyChannels', res.data.data.channels)
            setMyChannelsWithLocal(res.data.data.channels)
          }
          return [err, res]
        }
      }
    },
    // 在action中发请求，加载所有频道
    async loadAllChannels (ctx) {
      const [err, res] = await getAllChannel()
      if (!err) ctx.commit('setAllChannels', res.data.data.channels)
      return [err, res]
    },
    async addChannel (ctx, channels) {
      const resChannels = [...ctx.state.myChannels, channels]
      resChannels.forEach((v, i) => (v.seq = i + 1))
      if (ctx.rootState.user.token) {
        const [err, res] = await updateMyChannels(resChannels)
        if (!err) ctx.commit('setMyChannels', resChannels)
        return [err, res]
      } else {
        ctx.commit('setMyChannels', resChannels)
        setMyChannelsWithLocal(resChannels)
        const res = { data: { data: { channels: resChannels } } }
        return [null, res]
      }
    },
    async delChannel (ctx, id) {
      if (ctx.rootState.user.token) {
        const [err, res] = await delMyChannel(id)
        if (!err) {
          const resChannels = ctx.state.myChannels.filter(v => v.id !== id)
          ctx.commit('setMyChannels', resChannels)
        }
        return [err, res]
      } else {
        const resChannels = ctx.state.myChannels.filter(v => v.id !== id)
        setMyChannelsWithLocal(resChannels)
        return [null]
      }
    }
  },
  getters: {
    // 在getter中计算出可选频道列表
    getOptionsChannels (state) {
      return state.allChannels.filter(item => {
        return !state.myChannels.some(items => item.id === items.id)
      })
    },
    aaa () {
      return 1
    }
  }
}
